include $(TOPDIR)/rules.mk

PKG_NAME:=dnscrypt-proxy
PKG_VERSION:=1.6.0
PKG_RELEASE:=1

# PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

# PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2
# PKG_SOURCE_URL:=https://github.com/jedisct1/dnscrypt-proxy/releases/download/$(PKG_VERSION)/
# PKG_CAT:=bzcat

# PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
# PKG_SOURCE_URL:=http://download.dnscrypt.org/dnscrypt-proxy/
# PKG_CAT:=zcat

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/jedisct1/dnscrypt-proxy.git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_VERSION:=$(shell LC_ALL=C wget -O - $(PKG_SOURCE_URL) 2>/dev/null|grep '>latest commit'|grep -o '\w\{40\}')
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)/$(PKG_SOURCE_SUBDIR)

PKG_FIXUP:=autoreconf
PKG_INSTALL:=1

PKG_MAINTAINER:=Damiano Renfer <damiano.renfer@gmail.com>
PKG_LICENSE:=ISC

include $(INCLUDE_DIR)/package.mk

define Package/dnscrypt-proxy/Default
SECTION:=net
CATEGORY:=Network
SUBMENU:=IP Addresses and Names
DEPENDS:=+libsodium
URL:=http://dnscrypt.org/
MAINTAINER:=Damiano Renfer <damiano.renfer@gmail.com>
endef

define Package/dnscrypt-proxy
$(call Package/dnscrypt-proxy/Default)
TITLE:=A tool for securing communications between a client and a DNS resolver
endef

define Package/dnscrypt-proxy/description
dnscrypt-proxy provides local service which can be used directly as your
local resolver or as a DNS forwarder, encrypting and authenticating requests
using the DNSCrypt protocol and passing them to an upstream server.
The DNSCrypt protocol uses high-speed high-security elliptic-curve cryptography
and is very similar to DNSCurve, but focuses on securing communications between
a client and its first-level resolver.
endef

define Package/hostip
	$(call Package/dnscrypt-proxy/Default)
	TITLE:=Resolver to IPv4 or IPv6 addresses
endef

define Package/hostip/description
The DNSCrypt proxy ships with a simple tool named hostip that resolves a name
to IPv4 or IPv6 addresses.
endef

define Build/Configure
	$(call Build/Configure/Default, \
	--prefix=/usr \
	--disable-ssp \
	)
endef

TARGET_CFLAGS += \
	-fomit-frame-pointer \
	-fdata-sections \
	-ffunction-sections

TARGET_LDFLAGS += \
	-Wl,-gc-sections

MAKE_FLAGS += \
	CFLAGS="$(TARGET_CFLAGS)" \
	LDFLAGS="$(TARGET_LDFLAGS)"

define Package/dnscrypt-proxy/conffiles
	/etc/config/dnscrypt-proxy
endef

define Package/dnscrypt-proxy/postinst
#!/bin/sh
( . /etc/uci-defaults/dnscrypt-proxy ) && rm -f /etc/uci-defaults/dnscrypt-proxy
exit 0
endef

define Package/dnscrypt-proxy/prerm
#!/bin/sh
uci -q batch <<-EOF >/dev/null
	delete firewall.dnscrypt-proxy
	commit firewall
EOF
/etc/init.d/dnscrypt-proxy disable >/dev/null 2>&1
exit 0
endef

define Package/dnscrypt-proxy/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(CP) $(PKG_INSTALL_DIR)/usr/sbin/dnscrypt-proxy $(1)/usr/sbin/
	$(INSTALL_DIR) $(1)/usr/share/dnscrypt-proxy
	$(CP) $(PKG_INSTALL_DIR)/usr/share/dnscrypt-proxy/dnscrypt-resolvers.csv $(1)/usr/share/dnscrypt-proxy/
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/dnscrypt-proxy.init $(1)/etc/init.d/dnscrypt-proxy
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/dnscrypt-proxy.config $(1)/etc/config/dnscrypt-proxy
	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_BIN) ./files/dnscrypt-proxy.firewall $(1)/etc/firewall.dnscrypt-proxy
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/dnscrypt-proxy.uci $(1)/etc/uci-defaults/dnscrypt-proxy
endef

define Package/hostip/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(CP) $(PKG_INSTALL_DIR)/usr/bin/hostip $(1)/usr/bin/
endef

$(eval $(call BuildPackage,dnscrypt-proxy))
$(eval $(call BuildPackage,hostip))
